import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.EvaluatorException;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.JavaScriptException;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.WrappedException;

public class RunScript extends ScriptableObject {
	private static final long serialVersionUID = -5638074146250193112L;

	@Override
	public String getClassName() {
		return "global";
	}

	public static void main(String args[]) {
		// Creates and enters a Context. The Context stores information
		// about the execution environment of a script.
		Context cx = Context.enter();
		/*
		 * try { // Initialize the standard objects (Object, Function, etc.) //
		 * This must be done before scripts can be executed. Returns // a scope
		 * object that we use in later calls. Scriptable scope =
		 * cx.initStandardObjects();
		 * 
		 * // Collect the arguments into a single string. String s =
		 * "function f(x){var y = '<table/>'; y.td += <td>Ashok</td>; return y}; f(7)"
		 * ;
		 * 
		 * // Now evaluate the string we've colected. Object result =
		 * cx.evaluateString(scope, s, "<cmd>", 1, null);
		 * 
		 * // Convert the result to a string and print it.
		 * System.out.println(Context.toString(result));
		 * 
		 * } finally { // Exit from the context. Context.exit(); }
		 */
		ScriptableObject scope = cx.initStandardObjects();
		String[] names = { "print","readUrl" };
		scope.defineFunctionProperties(names, RunScript.class,
				ScriptableObject.DONTENUM);
		try {
			cx.evaluateReader(scope, new FileReader(
					"/Users/akuppuswamy8440/xre/mr-xre/theojs/js/some.js"),
					"test.js", 0, null);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public static void print(Context cx, Scriptable thisObj, Object[] args,Function funObj) {
		for (int i = 0; i < args.length; i++) {
			if (i > 0)
				System.out.print(" ");

			// Convert the arbitrary JavaScript value into a string form.
			String s = Context.toString(args[i]);

			System.out.print(s);
		}
		System.out.println();
	}
	
	public static String readUrl(Context cx, Scriptable thisObj, Object[] args,Function funObj) {
		String Url = (String)args[0];
		String result = "Hello";
		return result;
	}

}